#pragma once

#include "../code/server.hpp"


class Echo
{
public:
    Echo(uint16_t port)
        : _server(port)
    {
        _server.SetThreadCount(3);
        _server.EnableInactiveRelease(10);
        _server.SetConnectCallback(std::bind(&Echo::ConnectCallBack, this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&Echo::MessageCallBack, this, std::placeholders::_1, std::placeholders::_2));
        _server.SetClosedCallback(std::bind(&Echo::CloseCallback, this, std::placeholders::_1));
    }

    
    void Start()
    {
        _server.Start();
    }


private:
    void ConnectCallBack(const PtrConnection& conn)
    {
        LOG(Info, "链接创建成功 %p", conn.get());
    }

    void MessageCallBack(const PtrConnection& conn, Buffer* buf)
    {
        conn->Send((char*)buf->ReadPos(), buf->ReadableSize());
        buf->MoveReadOffset(buf->ReadableSize());
        // conn->Shundown();
    }

    void CloseCallback(const PtrConnection& conn)
    {
        LOG(Info, "close connection:%p", conn.get());
    }


private:
    TcpServer _server;    
};